package com.yunxi.service.dubboService.impl.dealer;

import com.aiways.center.channel.api.IChannelApi;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dtyunxi.rest.RestResponse;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dealer.pojo.PartnerStorageVO;
import com.yunxi.dealer.service.PartnerStorageDubboService;
import com.yunxi.model.dealer.PartnerStorage;
import com.yunxi.service.localService.dealer.PartnerStorageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 渠道商仓库地址 服务实现类
 * </p>
 *
 * @author
 * @since 2019-05-14
 */
//@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
//        application = "${dubbo.application.id}",
//        protocol = "${dubbo.protocol.id}",
//        registry = "${dubbo.registry.id}"
//)
public class PartnerStorageDubboServiceImpl implements PartnerStorageDubboService {

   private static final Logger logger = LoggerFactory.getLogger(PartnerInvoiceDubboServiceImpl.class);

   @Autowired
   @Qualifier(value = "partnerStorageServiceImpl")
   PartnerStorageService partnerStorageService;

   @Autowired
   IChannelApi channelApi;


   @Override
   public PartnerStorageVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      PartnerStorage partnerStorage = partnerStorageService.getById(id);
      PartnerStorageVO partnerStorageVO = new PartnerStorageVO();
      BeanUtils.copyProperties(partnerStorage, partnerStorageVO);
      return partnerStorageVO;
   }

   @Override
   public void saveOrUpdate(PartnerStorageVO partnerStorageVO) throws InvocationTargetException, IllegalAccessException {
      PartnerStorage partnerStorage = new PartnerStorage();
      BeanUtils.copyProperties(partnerStorageVO, partnerStorage);
      partnerStorageService.saveOrUpdate(partnerStorage);
   }

   @Override
   @Transactional
   public void removeById(String id, String partnerCode, LoginInfo loginInfo) {
      PartnerStorage partnerStorage = partnerStorageService.getById(id);
      partnerStorage.setUpdateBy(loginInfo.getRealName());
      //将渠道发运信息传入中台
      RestResponse<Void> voidRestResponse = null;
      try {
         voidRestResponse = channelApi.delChannelStorage(partnerStorage.getStorageCode());
      } catch (Exception e) {
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         logger.error(" ------- 中台调用出错 ___< 删除渠道发运信息  >--------"+e.getMessage());
         e.printStackTrace();
         return;
      }
      if (!voidRestResponse.getResultCode().equals("0")) {
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         logger.error(" ------- 中台执行失败 ___< 删除渠道发运信息  >--------"+voidRestResponse.getResultMsg());
         return;
      }

      partnerStorageService.removeById(id);
   }

   @Override
   public PageInfo<PartnerStorageVO> findPageByVO(PartnerStorageVO partnerStorageVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {

      PageInfo<PartnerStorageVO> pageInfo = partnerStorageService.pageByVo(partnerStorageVO, pageNum, pageSize);

      return pageInfo;

   }

   @Override
   public List listByVo(PartnerStorageVO v) {
      return partnerStorageService.listByVo(v);
   }

   @Override
   public PartnerStorageVO getByPartnerVO(String partnerCode) {
      return partnerStorageService.getByPartnerVO(partnerCode);
   }

}
